<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="mqtt-publish-regestration-behaviour.html">

<!--
The `mqtt-publish` element publishes MQTT messages to an MQTT topic. Place the element within a `<mqtt-connection>`
to create the binding between the connection and the element. The following example

    <mqtt-connection >
        <mqtt-publish
            auto
            retained
            qos="1"
            topic="foo/bar"
            payload="Hello world!"></mqtt-publish>
    </mqtt-connection>

publishes a retained message to the topic "foo/bar".

@demo demo/index.html
-->

<dom-module id="mqtt-publish">
  <template>
  </template>
</dom-module>

<script>
  'use strict';
  (function (scope) {

    var MqttElements = scope.MqttElements = scope.MqttElements || {};

    MqttElements.MqttPublish = Polymer({
      is: 'mqtt-publish',

      /**
       * Fired when a publication is published.
       *
       * @event mqtt-publication-published
       * @param {{
     *   topic:  string,
     *   qos: number,
     *   payload: string | Object
     *   retained: boolean
     * }}
       */

      properties: {

        /**
         * If auto is set to true `<mqtt-publish auto>` then an MQTT message is send each time
         * the payload is changed.
         */
        auto: {
          type: Boolean,
          value: false,
        },

        /**
         * The quality of service the MQTT publication is send to the MQTT broker
         */
        qos: {
          type: Number,
          value: 0,
        },

        /**
         * The topic the publication is made to
         */
        topic: {
          type: String,
        },

        /**
         * The payload that is send to the topic
         */
        payload: {
          type: Object,
          value: {},
        },

        /**
         * Set retained to true if the message should be saved at the MQTT broker an should "stick" on it.
         */
        retained: {
          type: Boolean,
          value: false,
        },

        /**
         * True if the publication was successful
         */
        published: {
          type: Boolean,
          value: false,
          readOnly: true,
        },
      },

      behaviors: [MqttElements.MqttPublishRegestrationBehaviour],

      observers: [
        '_propertiesChanged(topic, payload, qos, retained, _parentConnection)'
      ],

      /**
       * A property observer to send messages if `auto` is set to true and values change
       * @param topic
       * @param payload
       * @param qos
       * @param retained
       * @private
       */
      _propertiesChanged: function (topic, payload, qos, retained, _parentConnection) {
        if(this.auto && topic && payload && this._parentConnection) {
          this.publish();
        }
      },

      /**
       * Manually publishes the <mqtt-publish> element to the <mqtt-connection> it is bound to. Use the attributes on the
       * Element to set the topic, payload, QoS an retained flag for the MQTT message.
       */
      publish: function () {
        if(this.topic && this.topic.length > 0 && this.payload && this.payload.length > 0 && this._parentConnection) {
          this._setPublished(false);
          this._parentConnection.publish(this.topic, this.payload, this.qos, this.retained, this._handelPublicationPublished.bind(this));
        }
      },

      /**
       * Callback handel to notify the element that the payload has been published
       * @private
       */
      _handelPublicationPublished: function () {
        this._setPublished(true);

        this.fire(
            "mqtt-publication-published",
            {
              qos: this.qos,
              topic: this.topic,
              payload: this.payload,
              retained: this.retained
            }
        );
      },

    })
  })(window);
</script>
